const $cache = require('../../utils/storage')
const $api = require('../../utils/api').API
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    invitedCode:''
  },
  handleInput(e) {
    console.log(e)
    this.setData({
      userName: e.detail.value
    })
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    wx.login({
      success: (res1) => {
        console.log(res1, 999)
        let code = res1.code
        $api.login.login({
          code
        }).then(res2 => {
          let userInfo = res2.data.data
          $cache._setStorage('openId', userInfo.openId)
          $cache._setStorage('userInfo', userInfo)
          wx.uploadFile({
            url: 'https://qdwlkj.cn/account/upPersonImg',
            filePath: avatarUrl,
            name: "img",
            formData: {
              openId: userInfo.openId, //其他需要携带的参数
            },
            header: {
              "content-type": "multipart/form-data"
            },
            success: (res) => {
              $api.updateInfo({
                openId: res2.data.data.openId,
                username: this.data.userName,
                sex: '男',
                invitedCode: this.data.invitedCode
              })
              userInfo.username = this.data.userName
              userInfo.headSculpture = avatarUrl
              $cache._setStorage('userInfo', userInfo)
              wx.switchTab({
                url: '/pages/home_new/home_new'
              })

            },

          })

        })
      },
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      invitedCode:options.scene
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})